#ifndef DISPATHCER_H
#define DISPATHCER_H

#include "gp.h"
#include "correspond.h"
#include <string>
#include <vector>
#include <map>

using std::string;
using std::vector;
using std::map;

 typedef struct tag_ip_info{
         int ip[4];
         int  mask;
 }ip_info;

typedef struct{
	string ip;
//	int linknum;
}source_node;

class Dispatcher{

private:
//	Correspond corres;
	map<string,vector<source_node> >index;
	map<string,int> loadmap;	//record each leaf node's load;

public:
	Dispatcher(){};
	~Dispatcher(){};

/********** These two functions have been moved to Correspond ********/

//	int sendFile(char *filename);
//	int receiveFile();

/*********************************************************************/

/*********** Just for sending source-info to his father **************/

	int mapToFile(char* filename, int s_level);
	int fileToMap(char* filename);

/*********************************************************************/

/*************** Judge if a ip address in a ip segment ***************/

	int convert_ip_info(char* szIP, ip_info* pInfo);
	int convert_seg_info(const char* szIP, ip_info* pInfo);    
	int is_ip_in_seg(const ip_info *pIP, const ip_info* pSeg);
	int match_ip(const char *seg,char *ip);

/*********************************************************************/
	int findFit(char* filename,char ip[]);	
	int has_source(char* filename);

	/* used in loadmap */
	void initRS();
	void mapToStr(char str[]);
	void strToMap(char str[]);
	void setLoad(string ip,int load) { loadmap[ip] = load; }

	void addIndex(char* ip,char* filename);
	void deleteIndex(char* ip,char* filename);
	int deleteNode(string ip);
};

#endif
